package com.szxx.domain;

import com.szxx.constans.PokerGameConstant;
import com.szxx.suit.DdzTypeUtil;

/**
 * 牌 <br>
 *
 * @author Ambrose
 */
public class DdzCard extends Card {
	/** 是否癞子 */
	private boolean isLazi = false;
	/** 癞子类型 默认0地癞子，1天癞子 */
	private int laiziType;
	/** 癞子点数，默认为0为不赋值，此时读取number，可变 */
	private int laziNumber;
	
	public static void main(String[] args) {
		DdzCard dcard = new DdzCard(CardShape.DIAMOND, 5, true);
		DdzCard dcard2 = new DdzCard(CardShape.DIAMOND, 5, false);
		dcard2.changeCard(9);
		System.out.println(dcard.equals(dcard2));
	}

	public DdzCard(CardShape cardShape, int number) {
		super(cardShape, number);
	}
	
	public DdzCard(CardShape cardShape, int number, boolean isLazi) {
		super(cardShape, number);
		this.isLazi = isLazi;
	}
	
	@Override
	public Card revalue() {
		if (isLazi && laziNumber != 0) { // 有癞子点数的时候重新定义value
			if (laziNumber == 1) {
				setValue(PokerGameConstant.NUMBER_1_VALUE);
			} else if (laziNumber == 2) {
				setValue(PokerGameConstant.NUMBER_2_VALUE);
			} else {
				setValue(laziNumber);
			}
			return this;
		} else {
			if (getCardShape() == CardShape.RED_JOKER) { // 大王
				setLaziNumber(PokerGameConstant.RED_JOKER_SHAPE_NUM);
				setValue(PokerGameConstant.RED_JOKER_SHAPE_VALUE);
			} else if (getCardShape() == CardShape.BLACK_JOKER) { // 小王
				setLaziNumber(PokerGameConstant.BLACK_JOKER_SHAPE_NUM);
				setValue(PokerGameConstant.BLACK_JOKER_SHAPE_VALUE);
			} else {
				if (getNumber() == 1 ) {
					setValue(PokerGameConstant.NUMBER_1_VALUE);
				} else if (getNumber() == 2) {
					setValue(PokerGameConstant.NUMBER_2_VALUE);
				} else {
					setValue(getNumber());
				}
			}
			return this;
		}
	}
	
	/**
	 * 排序 null，牌，癞子
	 */
	@Override
	public int compareWithShapeTo(Card o) {
		if (o == null) {
			return 1;
		} else {
			DdzCard ddzO = (DdzCard) o;
			if (hasNotChanged() && ddzO.hasNotChanged()) { // 都是癞子
				return compareWithShapeLaiziTo(ddzO);
			} else if (hasNotChanged()) {
				return 1;
			} else if (ddzO.hasNotChanged()) {
				return -1;
			} else {
				return compareWithShapeLaiziTo(ddzO);
			}
		}
	}
	
	/**
	 * 带花色癞子的比较，用于排序 <br>
	 * 黑红梅方 value 小的在前面
	 * 癞子还在黑桃前
	 * 
	 * @param o
	 * @return
	 */
	public int compareWithShapeLaiziTo(Card o) {
		if (o == null) {
			return 1;
		} else {
			int weight_c = getValue() - o.getValue();
			if (weight_c == 0) {
				DdzCard ddzO = (DdzCard) o;
				if (isLazi && ddzO.isLazi) {
					if (o.getCardShape() == null) {
						return 1;
					} else {
						return getCardShape().getShape() - o.getCardShape().getShape();
					}
				} else if (isLazi) {
					return 1;
				} else if (ddzO.isLazi) {
					return -1;
				} else {
					if (o.getCardShape() == null) {
						return 1;
					} else {
						return getCardShape().getShape() - o.getCardShape().getShape();
					}
				}
			} else {
				return weight_c;	
			}
		}
	}

	/**
	 * 是癞子牌，并且已经改变值
	 * 
	 * @return
	 */
	public boolean hasNotChanged() {
		return isLazi && laziNumber == 0;
	}
	
	/**
	 * 改变癞子牌
	 * 
	 * @param laziNumber
	 */
	public void changeCard(int valueOrNumber) { // 有癞子点数的时候重新定义value
		if (isLazi) {
			this.laziNumber = DdzTypeUtil.getNumberByValue(valueOrNumber);
			revalue();
		}
	}
	
	@Override
	public String toString() {
		if (isLazi) {
			return super.toString() + "*" + laziNumber;
		} else {
			return super.toString();
		}
	}
	
	public boolean isLazi() {
		return isLazi;
	}

	public int getLaiziType() {
		return laiziType;
	}

	public void setLaiziType(int laiziType) {
		this.laiziType = laiziType;
	}

	public void setLazi(boolean isLazi) {
		this.isLazi = isLazi;
	}

	public int getLaziNumber() {
		return laziNumber;
	}
	
	public void setLaziNumber(int laziNumber) {
		this.laziNumber = laziNumber;
	}

}
